﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Toolkit.Web.Infastructure;
using System.Web;

namespace Toolkit.Web.ViewComponents.WebAssetRegistrar
{
    public class ScriptRegistrar : ViewComponentBase
    {
        public ScriptRegistrar(IViewComponentWriter writer)
            : base(writer)
        {
            if (HttpContext.Current.Items[Key] != null)
            {
                throw new InvalidOperationException("Only one script registrar is allowed in a single request.");
            }

            HttpContext.Current.Items[Key] = this;
            Assets = new WebAssetCollection();
            AssetsToIgnore = new WebAssetCollection();
            InlineScripts = new List<Action<IViewComponentWriter>>();

            Assets.Insert(0, new WebAssetGroup(DefaultGroupName, false));
        }

        public string Key
        {
            get
            {
                return typeof(ScriptRegistrar).AssemblyQualifiedName;
            }
        }

        public string DefaultGroupName
        {
            get
            {
                return "default";
            }
        }

        public WebAssetCollection Assets
        {
            get;
            private set;
        }

        public WebAssetCollection AssetsToIgnore
        {
            get;
            private set;
        }

        public IList<Action<IViewComponentWriter>> InlineScripts
        {
            get;
            private set;
        }
    }
}
